package github.sf.fw.tools.db.sql;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;

import java.util.List;
import java.util.stream.Collectors;

public class MysqlSqlUtil {
    public static String addSchema(String sql, String schema) {
        MySqlStatementParser parser = new MySqlStatementParser(sql);
        List<SQLStatement> sqlStatements = parser.parseStatementList();
        ModifyTableSourceVisitor visitor = new ModifyTableSourceVisitor();
        visitor.setDatabase(schema);
        for (SQLStatement sqlStatement : sqlStatements) {
            sqlStatement.accept(visitor);
        }
        return sqlStatements.stream().map(SQLStatement::toString).collect(Collectors.joining());
    }
}
